/*
 Copyright (C) 2012  Nikos Siatras
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
package ncore_tcp_port_forward.UI;

import java.util.Arrays;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import ncore_tcp_port_forward.Core.TCPForward;

/**
 *
 * @author Nikos Siatras
 */
public class frmForwardAccessList extends javax.swing.JFrame
{

    private frmMain fMainForm;
    private TCPForward fMyForward;

    public frmForwardAccessList(frmMain mainForm, TCPForward forward)
    {
        fMainForm = mainForm;
        fMyForward = forward;


        initComponents();

        if (fMyForward.getAccessList().isEnabled())
        {
            this.jCheckBoxEnableAccessList.setSelected(true);
        }

        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < fMyForward.getAccessList().getAccessList().size(); i++)
        {
            model.addElement(fMyForward.getAccessList().getAccessList().get(i));
        }
        jListAllowedHosts.setModel(model);

        this.setTitle("Access List '" + fMyForward.getName() + "'");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBoxEnableAccessList = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListAllowedHosts = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButtonRemoveHost = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jCheckBoxEnableAccessList.setText("Enable Access List");

        jLabel1.setText("Allowed Hosts:");

        jScrollPane1.setViewportView(jListAllowedHosts);

        jButton1.setText("Add Host");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Save");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButtonRemoveHost.setText("Remove Host");
        jButtonRemoveHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveHostActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jCheckBoxEnableAccessList)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonRemoveHost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBoxEnableAccessList)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemoveHost)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Save
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
        fMyForward.getAccessList().setEnabled(this.jCheckBoxEnableAccessList.isSelected());

        fMyForward.getAccessList().ClearAccessList();
        DefaultListModel model = (DefaultListModel) jListAllowedHosts.getModel();
        for (int i = 0; i < model.getSize(); i++)
        {
            fMyForward.getAccessList().AddToAccessList(model.get(i).toString());
        }

        fMainForm.Save();

        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Cancel
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
    {//GEN-HEADEREND:event_jButton3ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    // Add Host
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        String host = JOptionPane.showInputDialog(null, "IP Address or Host Name to add:", "Add Host", JOptionPane.PLAIN_MESSAGE);
        host = host.trim();

        try
        {
            if (host != null && !host.equals(""))
            {
                DefaultListModel model = (DefaultListModel) jListAllowedHosts.getModel();
                model.addElement(host);
                this.jListAllowedHosts.setModel(model);
            }
        }
        catch (Exception ex)
        {
            System.err.println(ex.getMessage());
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    // Remove Host
    private void jButtonRemoveHostActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveHostActionPerformed
    {//GEN-HEADEREND:event_jButtonRemoveHostActionPerformed
        DefaultListModel model = (DefaultListModel) jListAllowedHosts.getModel();
        int[] selectedIndices = jListAllowedHosts.getSelectedIndices();

        DefaultListModel newModel = new DefaultListModel();

        for (int i = 0; i < model.getSize(); i++)
        {
            if (Arrays.binarySearch(selectedIndices, i) < 0)
            {
                newModel.addElement(model.get(i));
            }
        }
        jListAllowedHosts.setModel(newModel);

    }//GEN-LAST:event_jButtonRemoveHostActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonRemoveHost;
    private javax.swing.JCheckBox jCheckBoxEnableAccessList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jListAllowedHosts;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
